<?php
namespace app\admin\controller\final_accounts;

use app\admin\traits\Curd;
use app\admin\model\FinalAccountsType;
use app\common\controller\AdminController;
use think\App;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\annotation\ControllerAnnotation;

/**
 * Class Type
 * @package app\admin\controller\final_accounts
 * @ControllerAnnotation(title="决算类型")
 */

class Type extends AdminController
{
    use Curd;
    public $type_arr = [
        '当期现金净流入'=> 1,
        '当期支出'=> 2,
        '当期毛利'=> 3,
        '当期毛利率'=> 4,
        '当期负债'=> 5,
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new FinalAccountsType();
    }



    

     /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            $rule = [
                'time|日期'   => 'require',
            ];
            $this->validate($post, $rule);
            try {
               
                $post['time'] = strtotime($post['time']);
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        
        $this->assign('type_arr', array_flip($this->type_arr));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'time|日期'   => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $post['time'] = strtotime($post['time']);
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $row['time'] = date('Y-m',$row['time']);
        $this->assign('type_arr', array_flip($this->type_arr));
        $this->assign('row', $row);
        return $this->fetch();
    }


    


}





































?>